import { Engine } from "../Engine";
import { any } from "../core/logic";
import { Component, ComponentType } from "./Component";
/**
 * @memberof w.component
 */
class MeshRender extends Component {
    constructor(opts = {}) {
        opts.type = any(opts.type, ComponentType.MeshRender);
        super(opts);
        this.meshTask = new Set();
        this.groups = new Map();
        this.meshes = new Map();
        this.bundles = [];//每次重新打包会重新生成该对象数组。
        this.autoUpdate = true;
    }
    addValueChange(meshTask) {//值改变任务化，再toGPU时需要判断是否销毁。
        this.meshTask.add(meshTask);
        this.update("value");
    }
    get(id){
        return this.meshes.get(id);
    }
    add(renderinfo) {//结构改变
        const { id, hash, groups } = renderinfo;
        this.meshes.set(id, renderinfo);
        for (let i = 0; i < groups.length; i++) {
            let group = groups[i];
            if (!this.groups.has(group)) {//没有group则创建group
                this.groups.set(group, { archetypes: new Map() });
            }
            this.groups.get(group).state = false;

            const archetypes = this.groups.get(group).archetypes;
            if (!archetypes.has(hash)) {//没有archetype则创建archetype
                archetypes.set(hash, {id:hash, meshes: new Set() });
            }
            archetypes.get(hash).state = false;
            if(Engine.instance.shadowEnable){
              archetypes.get(hash).stateShadow = false;
              archetypes.get(hash).stateShadowPoint = false;
            }
            const meshes = archetypes.get(hash).meshes;
            meshes.add(id);
        }
        this.update("layout");
    }
    updateState(){
        this.setState(true);
    }
    clear(){
        this.bundles=[];
        this.update("layout");
    }
    remove(renderinfo) {
        const { id, hash, groups } = renderinfo;
        this.meshes.delete(id)
        for (let i = 0; i < groups.length; i++) {
            const group=groups[i];
            let dgroup = this.groups.get(group);
            if (!dgroup) {
                return;
            }
            let darchetype = dgroup.archetypes.get(hash);
            if (!darchetype) {
                return;
            }
            let meshes = darchetype.meshes;
            if (!meshes) {
                return;
            }
            if (meshes.has(id)) {
                meshes.delete(id);
                if (meshes.size <= 0) {
                    dgroup.archetypes.delete(hash);
                    if (dgroup.size <= 0) {
                        this.groups.delete(group);
                    } else {
                        dgroup.state = false;
                    }
                } else {
                    darchetype.state = false;
                    if(Engine.instance.shadowEnable){
                      darchetype.stateShadow = false;
                      darchetype.stateShadowPoint = false;
                    }
                    dgroup.state = false;
                }
            }
        }
        this.update("layout");
    }
}
export { MeshRender }